<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Create a new html block object (div with 1 or 2 columns)
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */


/**
 * Create a new html block object (div with 1 or 2 columns)
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */
class JFX_Form_Block
{
    /**
     * HTML id name
     * @var string $id
     */
    protected $id;
    /**
     * Specify whether it is a one or two column block
     * @var string $colType
     */
    protected $colType;
    /**
     * Array containing all column one elements
     * @var array $col1
     */
    protected $col1 = array();
    /**
     * Array containing all column two elements
     * @var array $col2
     */
    protected $col2 = array();
    /**
     * array containing all HTML class names
     * @var array $classnames
     */
    protected $classnames = array();

    /**
     * Set the initial values for a new instance
     * @param string $colType
     * @param string $id
     */
    public function __construct($colType='full', $id=''){
        if($id=='')$id = 'form_block'.rand();
        $this->id = $id;
        $this->setColType($colType);
    }

    /**
     * Set the block type as being either one or two columns
     * valid values: "full" or "double"
     * @param string $type
     */
    public function setColType($type='full'){
        $this->colType = $type;
    }

    /**
     * Add an HTML class name to the block
     * @param string $classname
     */
    public function addClass($classname){
        $this->classnames[] = $classname;
    }

	/**
	 * Adds a fieldset to the block.
	 * $legend = html legend to display in the fieldset
	 * $column = 'left', 'right', or 'default'
     * @param string $legend
     * @param string $columnIndex
     * @return object JFX_Form_Fieldset
	 */
    public function add($legend, $column='default'){
        if($column === 'default' && $this->colType === 'full'){
            // just add the fieldset to the col1 array ( only using one column )
            $obj = new JFX_Form_Fieldset($legend);
            $obj->setId($this->id.count($this->col1).count($this->col2));
            $obj->formObj = $this->formObj;
            $obj->blockObj = $this;
            $this->col1[] = $obj;
            return $obj;
        };
        if($column === 'default' && $this->colType === 'double'){
            // find which has the least fieldset height
            $col1height = 0;
            foreach($this->col1 as $col){
                $col1height += $col->countComponents();
            };
            $col2height = 0;
            foreach($this->col2 as $col){
                $col2height += $col->countComponents();
            };
            if($col1height == 0 || $col1height < $col2height)
                return $this->add($legend, 'left');
            else
                return $this->add($legend, 'right');
        };
        if($column === 'left'){
            $this->col1[] = new JFX_Form_Fieldset($legend);
            $obj = $this->col1[(count($this->col1)-1)];
            $obj->setId($this->id.rand());
            $obj->formObj = $this->formObj;
            $obj->blockObj = $this;
            return $obj;
        };
        if($column === 'right'){
            $this->col2[] = new JFX_Form_Fieldset($legend);
            $obj = $this->col2[(count($this->col2)-1)];
            $obj->setId($this->id.rand());
            $obj->formObj = $this->formObj;
            $obj->blockObj = $this;
            return $obj;
        };
    }

    /**
     * Set the HTML id
     * @param string $id
     */
    public function setId($id){
        $this->id = $id;
    }

    /**
     * Get the HTML id
     * @return string $id
     */
    public function getId(){
        return $this->id;
    }

    /**
     * Return the block with all child elements as an HTML string
     * @return string $htmlString
     */
    public function render(){
        $returnString = "\n".'<div class="formBlock '.$this->colType.' '.implode(' ', $this->classnames).'" id="'.$this->id.'">';
        switch($this->colType){
            case 'full':
                foreach($this->col1 as $col){
                    $returnString .= "\n  ".'<div class="fieldsetHolder">'.$col->render()."</div>".'<div class="clear"></div>'."\n";
                };
            break;
            case 'double':
                $returnString .= "\n".'  <div class="double-left">';
                foreach($this->col1 as $col){
                    $returnString .= "\n    ".'<div class="fieldsetHolder">'.$col->render().'</div>'.'<div class="clear"></div>';
                };
                $returnString .= "\n  </div><!-- end .double-left -->\n".'  <div class="double-right">';
                foreach($this->col2 as $col){
                    $returnString .= "\n    ".'<div class="fieldsetHolder">'.$col->render().'</div>'.'<div class="clear"></div>';
                };
                $returnString .= "\n  </div><!-- end .double-right -->\n";
            break;
        };
        $returnString .= "\n".'<div class="clear"></div>'.'</div><!-- end .block.'.$this->colType.' -->';
        return $returnString;
    }

}
